package com.derbysoft.synxisadapter.integration.dswitch3;

import com.derbysoft.core.utils.DateTimeUtils;
import com.derbysoft.dswitch.dto.hotel.avail.RoomStayCandidateDTO;
import com.derbysoft.dswitch.dto.hotel.common.*;
import com.derbysoft.dswitch.dto.hotel.reservation.HotelReservationDTO;
import com.derbysoft.dswitch.dto.hotel.reservation.ReservationRoomStayDTO;
import com.derbysoft.dswitch.remote.hotel.buyer.DefaultHotelBuyerRemoteService;
import com.derbysoft.dswitch.remote.hotel.dto.RequestHeader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class IntegrationTestSupport extends TestSupport {

    private static final String CHANNEL_RESERVATION_NO = "L2N8888888";
    protected Log logger = LogFactory.getLog(getClass());

    private static final int CONNECTION_SIZE = 1024;
    //private static final String SOURCE = "HotelBeds";
    //private static final String SOURCE = "GTA";
    private static final String SOURCE = "Qunar";
    //private static final String SOURCE = "MMTRP";

    private static final String HOTEL_CODE = "11206";
    private static final String CHECK_IN = "2013-08-04";
    private static final int LOS = 3;
    private static final Integer NUMBER_OF_UNIT = 1;
    private static final Integer ADULT = 2;
    private static final Integer CHILD = 0;
    protected static final String LANGUAGE = "EN_US";

    private static final String RATE_PLAN_CODE = "DISC";
    private static final String RATE_PLAN_NAME = "NAME:DISC";
    private static final String ROOM_TYPE_CODE = "PRS";
    private static final String ROOM_TYPE_NAME = "NAME:PRS";

    //    private static final String URL = "127.0.0.1:9002";
//    private static final String URL = "175.41.236.114:9002";  //Production
    //private static final String URL = "54.248.108.75:9003";  //synxis-adapter qa
    private static final String URL = "54.248.1.255:9002";  //dswitch3 qa

    protected static DefaultHotelBuyerRemoteService remoteService =
            new DefaultHotelBuyerRemoteService(URL, CONNECTION_SIZE);

    static {
        remoteService.setNoCacheAvailTimeout(100000);
        remoteService.setAvailTimeout(100000);
        remoteService.setNoCacheAvailTimeout(150000);
        remoteService.setReservationTimeout(150000);
    }

    protected RequestHeader createHeader() {
        RequestHeader header = new RequestHeader();
        header.setTaskId(UUID.randomUUID().toString());
        header.setSource(SOURCE);
        header.setDestination("SYNXIS");
        return header;
    }

    protected List<String> createHotelPassport() {
        ArrayList<String> hotelCodes = new ArrayList<String>();
        hotelCodes.add(HOTEL_CODE);
        return hotelCodes;
    }

    protected StayDateRangeDTO createDateRange() {
        return new StayDateRangeDTO(CHECK_IN, DateTimeUtils.formatDate(DateTimeUtils.addHours(DateTimeUtils.parse(CHECK_IN), LOS * 24)));
    }

    protected RoomStayCandidateDTO createRoomStayCandidate() {
        RoomStayCandidateDTO roomStayCandidateDTO = new RoomStayCandidateDTO();
        roomStayCandidateDTO.setAdultCount(ADULT);
        roomStayCandidateDTO.setChildCount(CHILD);
        roomStayCandidateDTO.setNumberOfUnits(NUMBER_OF_UNIT);
        return roomStayCandidateDTO;
    }

    protected HotelReservationDTO createHotelReservation() {
        HotelReservationDTO reservationDTO = new HotelReservationDTO();
        reservationDTO.setHotelCode(HOTEL_CODE);
        reservationDTO.setLanguage(LANGUAGE);
        reservationDTO.setChannelReservationNo(CHANNEL_RESERVATION_NO);
        reservationDTO.setRoomStaysList(createRoomStay());
        return reservationDTO;
    }

    private List<ReservationRoomStayDTO> createRoomStay() {
        List<ReservationRoomStayDTO> reservationRoomStayDTOs = new ArrayList<ReservationRoomStayDTO>();
        ReservationRoomStayDTO reservationRoomStayDTO = new ReservationRoomStayDTO();
        reservationRoomStayDTO.setStayDateRange(createDateRange());
        reservationRoomStayDTO.setRatePlanCode(RATE_PLAN_CODE);
        reservationRoomStayDTO.setRatePlanName(RATE_PLAN_NAME);
        reservationRoomStayDTO.setRoomTypeCode(ROOM_TYPE_CODE);
        reservationRoomStayDTO.setRoomTypeName(ROOM_TYPE_NAME);
        reservationRoomStayDTO.setAdultCount(ADULT);
        reservationRoomStayDTO.setChildCount(CHILD);
        reservationRoomStayDTO.setNumberOfUnits(NUMBER_OF_UNIT);
        reservationRoomStayDTO.setGuestsList(createGuests());
        reservationRoomStayDTO.setCommentsList(createComments());
        reservationRoomStayDTO.setSpecialRequestsList(createSpecialRequests());
        reservationRoomStayDTO.setGuaranteesList(createGuarantees());
        reservationRoomStayDTO.setContactPersonsList(createPersons());
        reservationRoomStayDTOs.add(reservationRoomStayDTO);
        return reservationRoomStayDTOs;
    }

    private List<CustomerDTO> createPersons() {
        ArrayList<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
        CustomerDTO customerDTO = new CustomerDTO();
        PersonNameDTO personName = new PersonNameDTO();
        personName.setSurname("Green");
        personName.setGivenName("Jim");
        customerDTO.setPersonName(personName);
        ArrayList<String> emails = new ArrayList<String>();
        emails.add("jim@gmail.com");
        customerDTO.setEmailsList(emails);
        ArrayList<TelephoneDTO> telephones = new ArrayList<TelephoneDTO>();
        TelephoneDTO telephoneDTO = new TelephoneDTO();
        telephoneDTO.setLocationType(TelephoneDTO.PhoneLocationType.HOME);
        telephoneDTO.setNumber("13888888888");
        telephoneDTO.setTechType(TelephoneDTO.PhoneTechType.VOICE);
        telephones.add(telephoneDTO);
        customerDTO.setTelephonesList(telephones);
        customerDTOs.add(customerDTO);
        return customerDTOs;
    }

    private List<GuaranteeDTO> createGuarantees() {
        ArrayList<GuaranteeDTO> guaranteeDTOs = new ArrayList<GuaranteeDTO>();
        GuaranteeDTO guaranteeDTO = new GuaranteeDTO();
        guaranteeDTO.setPaymentCard(createPaymentCard());
        guaranteeDTO.setGuaranteeType(GuaranteeDTO.GuaranteeType.CreditCard);
        guaranteeDTOs.add(guaranteeDTO);
        return guaranteeDTOs;
    }

    private PaymentCardDTO createPaymentCard() {
        return new PaymentCardDTO(PaymentCardDTO.CardType.CREDIT, "VI", "4111111111111111", "CardHolder", "1213");
    }

    private List<String> createSpecialRequests() {
        List<String> specialRequests = new ArrayList<String>();
        specialRequests.add("including coffee bar");
        return specialRequests;
    }

    private List<String> createComments() {
        List<String> comments = new ArrayList<String>();
        comments.add("no smoking");
        comments.add("low floor");
        return comments;
    }

    private List<CustomerDTO> createGuests() {
        List<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
        CustomerDTO customerDTO = new CustomerDTO();
        PersonNameDTO personName = new PersonNameDTO();
        personName.setSurname("wu");
        personName.setGivenName("jason");
        customerDTO.setPersonName(personName);
        ArrayList<String> emails = new ArrayList<String>();
        emails.add("bugstorms@gmail.com");
        customerDTO.setEmailsList(emails);
        ArrayList<TelephoneDTO> telephones = new ArrayList<TelephoneDTO>();
        TelephoneDTO telephoneDTO = new TelephoneDTO();
        telephoneDTO.setNumber("+86-21-33848552(1120)");
        telephoneDTO.setLocationType(TelephoneDTO.PhoneLocationType.OFFICE);
        telephoneDTO.setTechType(TelephoneDTO.PhoneTechType.VOICE);
        telephones.add(telephoneDTO);
        customerDTO.setTelephonesList(telephones);
        customerDTOs.add(customerDTO);
        return customerDTOs;
    }
}
